package cn.cqzaier.pub.api.plugin.wechat;

import cn.cqzaier.pub.api.Result;
import cn.cqzaier.pub.api.plugin.WxrebootCommonApiInterface;
import cn.cqzaier.pub.api.plugin.base.reqeust.wechat.*;
import cn.cqzaier.pub.api.plugin.cuteCat.response.GroupInfo;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ReUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * 我的框架 VLM
 */
public class WxrebootWechartApiImpl implements WxrebootCommonApiInterface {
    /**
     * 文件路径匹配
     */
    public final static String REG_PIC_PATH = "([\\s\\w+:]*?/?(/.+/)?)((\\w+)\\.(\\w+))";

    public final static String REG_PIC_MSG = "\\[pic=([\\s\\w+:]*?/?(/.+/)?)((\\w+)\\.(\\w+))]$";


    private WechatRebootRequest wechatRebootRequest;
    private WechatRequestUtil wechatRequestUtil;


    @Override
    public String getRequestUrl() {
        return this.wechatRequestUtil.getRequstUrl();
    }


    public WxrebootWechartApiImpl(WechatRebootRequest wechatRebootRequest,WechatRequestUtil wechatRequestUtil) {
        this.wechatRebootRequest = wechatRebootRequest;
         this.wechatRequestUtil  = wechatRequestUtil;
    }

    @Override
    public Map<String, Object> getFriendlist(String rebootId) {
        this.wechatRebootRequest.setApi("GetFriendlist");
        return ((WechatRequestUtil<WechatRebootRequest, Map<String,Object>>)wechatRequestUtil).doReq(this.wechatRebootRequest,new Function<String, Map<String,Object>>() {
            @Override
            public Map<String,Object> apply(String s) {
                System.out.println();
                return null;
            }
        });
    }

    @Override
    public List<GroupInfo> getGrouplist(String rebootId) {
        this.wechatRebootRequest.setApi("GetGrouplist");
        return ((WechatRequestUtil<WechatRebootRequest,List<GroupInfo>>)wechatRequestUtil).doReq(this.wechatRebootRequest, new Function<String, List<GroupInfo>>() {
            @Override
            public List<GroupInfo> apply(String s) {
                if (wechatRequestUtil.isHealth()){
                    return new Result<GroupInfo>().getResultArryByKeySplitDone(s,GroupInfo.class,"ReturnJson");
                }
                return null;
            }
        });
    }

    @Override
    public JSONObject sendGroupTxtMsg(String rebootId,String groupId,String msg) {
        this.wechatRebootRequest.setApi("SendTextMsg");
        this.wechatRebootRequest.setTo_wxid(groupId);
        this.wechatRebootRequest.setMsg(msg);
        if ( !CollectionUtil.isEmpty(ReUtil.findAll(REG_PIC_MSG, msg, 1))){ //  图片路径文本匹配
            return sendImageMsg(rebootId,groupId,ReUtil.findAllGroup0(REG_PIC_PATH, msg).get(0));
        }
        return ((WechatRequestUtil<WechatRebootRequest,JSONObject>)wechatRequestUtil).doReq(this.wechatRebootRequest, new Function<String, JSONObject>() {
            @Override
            public JSONObject apply(String s) {
                if (wechatRequestUtil.isHealth()){
                    return JSONObject.parseObject(s,JSONObject.class);
                }
                return null;
            }
        });
    }

    @Override
    public JSONObject sendPrivateTxtMsg(String rebootId, String groupId, String msg) {
        return sendGroupTxtMsg(rebootId,groupId,msg);
    }

    @Override
    public JSONObject sendImageMsg(String rebootId, String groupId, String pic) {
        if ( !CollectionUtil.isEmpty(ReUtil.findAll(REG_PIC_MSG, pic, 1))){ //  图片路径文本匹配
            pic = ReUtil.findAllGroup0(REG_PIC_PATH, pic).get(0);
        }
        //#BUG 路径处理
        pic = pic.replaceAll("/", "\\\\");
        WechatSendFileAndImage wechatSendImage = new WechatSendFileAndImage();
        copyProperty(wechatSendImage);
        wechatSendImage.setPath(pic);
        wechatSendImage.setTo_wxid(groupId);
        wechatSendImage.setApi("SendImageMsg");
        return sendFileOrImage(wechatSendImage);
    }

    private void copyProperty(Object wechatSendImage) {
        BeanUtils.copyProperties(this.wechatRebootRequest, wechatSendImage);
    }

    private JSONObject sendFileOrImage(WechatSendFileAndImage wechatSendImage) {
        return ((WechatRequestUtil<WechatSendFileAndImage, JSONObject>) wechatRequestUtil).doReq(wechatSendImage, new Function<String, JSONObject>() {
            @Override
            public JSONObject apply(String s) {
                if (wechatRequestUtil.isHealth()) {
                    return JSONObject.parseObject(s, JSONObject.class);
                }
                return null;
            }
        });
    }


    @Override
    public JSONObject sendFile(String rebootId, String groupId, String pic) {
        WechatSendFileAndImage wechatSendImage = new WechatSendFileAndImage();
        copyProperty(wechatSendImage);
        wechatSendImage.setPath(pic);
        wechatSendImage.setTo_wxid(groupId);
        wechatSendImage.setApi("SendFileMsg");
        return sendFileOrImage(wechatSendImage);
    }

    @Override
    public JSONObject sendShareLink(String rebootId, String groupId, String title, String desc, String imageUrl, String url) {
        WechatShareLink wechatShareLink = new WechatShareLink();
        copyProperty(wechatShareLink);
        wechatShareLink.setApi("SendShareLinkMsg");
        wechatShareLink.setDesc(desc);
        wechatShareLink.setTo_wxid(groupId);
        wechatShareLink.setImage_url(imageUrl);
        wechatShareLink.setUrl(url);
        wechatShareLink.setTitle(title);
        return  ((WechatRequestUtil<WechatShareLink, JSONObject>) wechatRequestUtil).doReq(wechatShareLink, new Function<String, JSONObject>() {
            @Override
            public JSONObject apply(String s) {
                if (wechatRequestUtil.isHealth()) {
                    return JSONObject.parseObject(s, JSONObject.class);
                }
                return null;
            }
        });
    }

    @Override
    public List<JSONObject> getLoggedAccountList() {
        this.wechatRebootRequest.setApi("GetLoginWxid");
        this.wechatRebootRequest.setRobot_wxid(null);
        return  ((WechatRequestUtil<WechatRebootRequest, List<JSONObject>>) wechatRequestUtil).doReq(this.wechatRebootRequest, new Function<String, List<JSONObject>>() {
            @Override
            public List<JSONObject> apply(String s) {
                if (wechatRequestUtil.isHealth()) {
                   // return JSONObject.parseObject(s, JSONObject.class);
                }
                return null;
            }
        });
    }

    @Override
    public JSONObject sendGroupMsgAndAt(String rebootId, String groupId, String memberId, String msg) {
        WechatSendGroupAndAt wechatSendGroupAndAt = new WechatSendGroupAndAt();
        copyProperty(wechatSendGroupAndAt);
        wechatSendGroupAndAt.setApi("SendGroupMsgAndAt");
        wechatSendGroupAndAt.setGroup_wxid(groupId);
        wechatSendGroupAndAt.setMember_wxid(memberId);
        wechatSendGroupAndAt.setMsg(msg);
        return  ((WechatRequestUtil<WechatSendGroupAndAt, JSONObject>) wechatRequestUtil).doReq(wechatSendGroupAndAt, new Function<String, JSONObject>() {
            @Override
            public JSONObject apply(String s) {
                if (wechatRequestUtil.isHealth()) {
                    return JSONObject.parseObject(s, JSONObject.class);
                }
                return null;
            }
        });
    }

    @Override
    public JSONObject agreeFriendVerify(String rebootId, JSONObject jsonObject) {
//        WechatAgreeFriendVerify wechatAgreeFriendVerify = new WechatAgreeFriendVerify();
//        this.wechatSendGroupAndAt.setEvent(event);
//        this.wechatSendGroupAndAt.setRobot_wxid(rebootId);
//        this.cuteCatRebootRequest.setGroup_wxid(groupId);
//        this.cuteCatRebootRequest.setMember_wxid(memberId);
//        copyProperty(cuteCatTxtRebootRequest);
//        return msgTypeSend("AgreeFriendVerify",rebootId, null, null, null);
        return null;
    }

    @Override
    public JSONObject removeGroupMember(String rebootId, String groupId, String memberId, JSONObject otherInfo) {
        WechatSendGroupAndAt wechatSendGroupAndAt = new WechatSendGroupAndAt();
        copyProperty(wechatSendGroupAndAt);
        wechatSendGroupAndAt.setApi("RemoveGroupMember");
        wechatSendGroupAndAt.setGroup_wxid(groupId);
        wechatSendGroupAndAt.setMember_wxid(memberId);
        return  ((WechatRequestUtil<WechatSendGroupAndAt, JSONObject>) wechatRequestUtil).doReq(wechatSendGroupAndAt, new Function<String, JSONObject>() {
            @Override
            public JSONObject apply(String s) {
                if (wechatRequestUtil.isHealth()) {
                    return JSONObject.parseObject(s, JSONObject.class);
                }
                return null;
            }
        });
    }

    @Override
    public JSONObject sendVideo(String rebootId, String groupId, String videoUrl) {
        return null;
    }

    @Override
    public JSONObject sendGroupMsgAndAtAll(String rebootId, String groupId, String msg) {
        WechatSendGroupAndAtAll wechatSendGroupAndAt = new WechatSendGroupAndAtAll();
        copyProperty(wechatSendGroupAndAt);
        wechatSendGroupAndAt.setApi("SendMsgAtAll");
        wechatSendGroupAndAt.setGroup_wxid(groupId);
        wechatSendGroupAndAt.setMsg(msg);
        return  ((WechatRequestUtil<WechatRebootRequest, JSONObject>) wechatRequestUtil).doReq(wechatSendGroupAndAt, new Function<String, JSONObject>() {
            @Override
            public JSONObject apply(String s) {
                if (wechatRequestUtil.isHealth()) {
                    // return JSONObject.parseObject(s, JSONObject.class);
                }
                return null;
            }
        });

    }

    @Override
    public JSONObject downloadFileUrlLocal(String requestUrl,String url,String path){
        return null;
    }


    @Override
    public boolean revokeMsg(String rebootId, String msgId) {
         RevokeMsgRequest revokeMsgRequest = new RevokeMsgRequest();
        copyProperty(revokeMsgRequest);
        revokeMsgRequest.setApi("WithdrawOwnMessage");
        revokeMsgRequest.setMsgid(msgId);

        return ((WechatRequestUtil<RevokeMsgRequest, Boolean>) wechatRequestUtil).doReq(revokeMsgRequest, new Function<String,Boolean>() {
            @Override
            public Boolean apply(String s) {
                if (wechatRequestUtil.isHealth()) {
                      if (JSONObject.parseObject(s, JSONObject.class).getInteger("Code") == 0){
                          return true;
                      };
                }
                return false;
            }
        });
    }
}
